home *** CD-ROM | disk | FTP | other *** search
- head 1.3;
- branch ;
- access ;
- symbols ;
- locks ; strict;
- comment @ * @;
-
-
- 1.3
- date 92.06.10.17.24.12; author jhh; state Exp;
- branches ;
- next 1.2;
-
- 1.2
- date 89.01.11.08.53.05; author rab; state Exp;
- branches ;
- next 1.1;
-
- 1.1
- date 88.11.23.10.34.54; author rab; state Exp;
- branches ;
- next ;
-
-
- desc
- @@
-
-
- 1.3
- log
- @added definition of "debug" so common.c would compile
- @
- text
- @/*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
- */
-
- #ifndef lint
- char copyright[] =
- "@@(#) Copyright (c) 1983 Regents of the University of California.\n\
- All rights reserved.\n";
- #endif /* not lint */
-
- #ifndef lint
- static char sccsid[] = "@@(#)lpc.c 5.5 (Berkeley) 5/5/88";
- #endif /* not lint */
-
- /*
- * lpc -- line printer control program
- */
- #include <stdio.h>
- #include <signal.h>
- #include <ctype.h>
- #include <setjmp.h>
- #include <syslog.h>
-
- #include "lpc.h"
-
- int fromatty;
-
- char cmdline[200];
- int margc;
- char *margv[20];
- int top;
- int intr();
- struct cmd *getcmd();
- extern int NCMDS;
-
- int debug = 0;
- jmp_buf toplevel;
-
- main(argc, argv)
- char *argv[];
- {
- register struct cmd *c;
- extern char *name;
-
- name = argv[0];
- openlog("lpd", 0, LOG_LPR);
-
- if (--argc > 0) {
- c = getcmd(*++argv);
- if (c == (struct cmd *)-1) {
- printf("?Ambiguous command\n");
- exit(1);
- }
- if (c == 0) {
- printf("?Invalid command\n");
- exit(1);
- }
- if (c->c_priv && getuid()) {
- printf("?Privileged command\n");
- exit(1);
- }
- (*c->c_handler)(argc, argv);
- exit(0);
- }
- fromatty = isatty(fileno(stdin));
- top = setjmp(toplevel) == 0;
- if (top)
- signal(SIGINT, intr);
- for (;;) {
- cmdscanner(top);
- top = 1;
- }
- }
-
- intr()
- {
- if (!fromatty)
- exit(0);
- longjmp(toplevel, 1);
- }
-
- /*
- * Command parser.
- */
- cmdscanner(top)
- int top;
- {
- register struct cmd *c;
-
- if (!top)
- putchar('\n');
- for (;;) {
- if (fromatty) {
- printf("lpc> ");
- fflush(stdout);
- }
- if (gets(cmdline) == 0)
- quit();
- if (cmdline[0] == 0)
- break;
- makeargv();
- c = getcmd(margv[0]);
- if (c == (struct cmd *)-1) {
- printf("?Ambiguous command\n");
- continue;
- }
- if (c == 0) {
- printf("?Invalid command\n");
- continue;
- }
- if (c->c_priv && getuid()) {
- printf("?Privileged command\n");
- continue;
- }
- (*c->c_handler)(margc, margv);
- }
- longjmp(toplevel, 0);
- }
-
- extern struct cmd cmdtab[];
-
- struct cmd *
- getcmd(name)
- register char *name;
- {
- register char *p, *q;
- register struct cmd *c, *found;
- register int nmatches, longest;
- int ncmds;
-
- longest = 0;
- nmatches = 0;
- found = 0;
- for (c = cmdtab, ncmds = NCMDS; --ncmds >= 0; c++) {
- p = c->c_name;
- for (q = name; *q == *p++; q++)
- if (*q == 0) /* exact match? */
- return(c);
- if (!*q) { /* the name was a prefix */
- if (q - name > longest) {
- longest = q - name;
- nmatches = 1;
- found = c;
- } else if (q - name == longest)
- nmatches++;
- }
- }
- if (nmatches > 1)
- return((struct cmd *)-1);
- return(found);
- }
-
- /*
- * Slice a string up into argc/argv.
- */
- makeargv()
- {
- register char *cp;
- register char **argp = margv;
-
- margc = 0;
- for (cp = cmdline; *cp;) {
- while (isspace(*cp))
- cp++;
- if (*cp == '\0')
- break;
- *argp++ = cp;
- margc += 1;
- while (*cp != '\0' && !isspace(*cp))
- cp++;
- if (*cp == '\0')
- break;
- *cp++ = '\0';
- }
- *argp++ = 0;
- }
-
- #define HELPINDENT (sizeof ("directory"))
-
- /*
- * Help command.
- */
- help(argc, argv)
- int argc;
- char *argv[];
- {
- register struct cmd *c;
-
- if (argc == 1) {
- register int i, j, w;
- int columns, width = 0, lines;
-
- printf("Commands may be abbreviated. Commands are:\n\n");
- for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
- int len = strlen(c->c_name);
-
- if (len > width)
- width = len;
- }
- width = (width + 8) &~ 7;
- columns = 80 / width;
- if (columns == 0)
- columns = 1;
- #ifdef sprite
- lines = (NCMDS + columns) / columns;
- #else
- lines = (NCMDS + columns - 1) / columns;
- #endif
- for (i = 0; i < lines; i++) {
- for (j = 0; j < columns; j++) {
- c = cmdtab + j * lines + i;
- printf("%s", c->c_name);
- if (c + lines >= &cmdtab[NCMDS]) {
- printf("\n");
- break;
- }
- w = strlen(c->c_name);
- while (w < width) {
- w = (w + 8) &~ 7;
- putchar('\t');
- }
- }
- }
- return;
- }
- while (--argc > 0) {
- register char *arg;
- arg = *++argv;
- c = getcmd(arg);
- if (c == (struct cmd *)-1)
- printf("?Ambiguous help command %s\n", arg);
- else if (c == (struct cmd *)0)
- printf("?Invalid help command %s\n", arg);
- else
- printf("%-*s\t%s\n", HELPINDENT,
- c->c_name, c->c_help);
- }
- }
- @
-
-
- 1.2
- log
- @*** empty log message ***
- @
- text
- @d44 1
- @
-
-
- 1.1
- log
- @Initial revision
- @
- text
- @d42 1
- d136 1
- d141 2
- a142 1
- for (c = cmdtab; p = c->c_name; c++) {
- a198 1
- extern int NCMDS;
- @
-